import subprocess
import os
from types import SimpleNamespace


filename = "x061_070.txt"

video_dir = r"V:\动漫\完美世界"

out_dir = r"V:\动漫\完美世界\out"

video_map = {}

all_merge_file = ""

single = True
single_merge = False
all_merge = False

def runCmd(cmd):
    subprocess.run(cmd, shell=True, check=True)

def writeMTxt(name):
    if all_merge:
        with open(all_merge_file, "a") as file:
            file.write("file 'o" + name + ".mp4'\n")

def deleteFile(filename):
    if ( os.path.exists(filename)):
        os.remove(filename)

with open(out_dir + "/" + filename, 'r') as file:
    while (line := file.readline()):
        info = line.strip().split(",")
        if( len(info) == 3 ):
            name = info[0]
            videoTime = SimpleNamespace()
            videoTime.start = info[1]
            videoTime.end = info[2]

            video = SimpleNamespace()
            video.times = []

            if( name in video_map ):
                video = video_map[name]

            video.name = name
            video.times.append(videoTime)

            video_map[name] = video

s = list(video_map.values())[0]
d = list(video_map.values())[-1]

all_merge_name = "z" + s.name + "_" + d.name + ".txt"
all_merge_file = out_dir + "/" + all_merge_name
if all_merge:
    deleteFile(all_merge_file)

for name,video in video_map.items():
    if ( len(video.times) == 1 ):
        time = video.times[0]
        cmd = "ffmpeg -ss {start} -to {end} -i {name}.mp4 -c copy out/o{name}.mp4".format(name=video.name,start= time.start,end=time.end)
        print(cmd)
        video_file = out_dir + "/o" + video.name + ".mp4"

        if os.path.exists(video_file) :
            continue

        if single :
            deleteFile(video_file)
            os.chdir(video_dir)
            runCmd(cmd)

        writeMTxt(video.name)

    elif( len(video.times) == 2 ):
        time0 = video.times[0]
        time1 = video.times[1]

        cmd0 = "ffmpeg -ss {start} -to {end} -i {name}.mp4 -c copy out/p{name}_0.mp4".format(name=video.name,start= time0.start,end=time0.end)
        cmd1 = "ffmpeg -ss {start} -to {end} -i {name}.mp4 -c copy out/p{name}_1.mp4".format(name=video.name,start= time1.start,end=time1.end)
        print(cmd0)
        print(cmd1)

        video_file0 = out_dir + "/p" + video.name + "_0.mp4"
        video_file1 = out_dir + "/p" + video.name + "_1.mp4"
        txt = out_dir + "/y" + video.name + ".txt"
        video_file = out_dir + "/o" + video.name + ".mp4"

        if os.path.exists(video_file0) :
            continue

        if single :
            deleteFile(video_file0)
            deleteFile(video_file1)
            os.chdir(video_dir)
            runCmd(cmd0)
            runCmd(cmd1)

        command = "ffmpeg -f concat -i y" + video.name + ".txt -c copy o" + video.name + ".mp4"
        print(command)

        if single and single_merge :
            deleteFile(video_file)
            deleteFile(txt)
            with open(txt, 'w') as file:
                file.write("file 'p" + video.name + "_0.mp4'\n")
                file.write("file 'p" + video.name + "_1.mp4'\n")

            os.chdir(out_dir)
            runCmd(command)

        writeMTxt(video.name)

    else:
        continue

m_file_name = s.name + "_" + d.name + ".mp4"
m_file = out_dir + "/" + m_file_name

m_command = "ffmpeg -f concat -i " + all_merge_file + " -c copy " + m_file_name
print(m_command)

if all_merge:
    deleteFile(m_file)
    os.chdir(out_dir)
    runCmd(m_command)






